/**
 * 
 */
package gestionBulletins.view;

import gestionBulletins.model.Section;
import gestionBulletins.model.persistance.DaoFactory;
import gestionBulletins.model.persistance.DaoSection;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

/**
 * @author paraita
 *
 */
public class AddSectionWindow extends JDialog implements ActionListener {

	private static final long serialVersionUID = 1L;
	protected DaoFactory daoFactory;
	protected InfosSectionWindow isw;
	protected JButton boutonAjouter;
	protected JButton boutonSortir;
	protected JTextField inputPresident;
	
	public AddSectionWindow(DaoFactory daoFactory, InfosSectionWindow isw) {
		this.daoFactory = daoFactory;
		this.isw = isw;
		initComposantsGraphiques();
		bindToListener();
		setupFrame();
		pack();
		/* position au milieu de l'écran */
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation((dim.width - getSize().width) / 2,
						 (dim.height - getSize().height) / 2);
	}
	
	private void initComposantsGraphiques() {
		boutonAjouter = new JButton("Ajouter");
		boutonSortir = new JButton("Fermer");
		inputPresident = new JTextField();
	}
	
	private void bindToListener() {
		boutonAjouter.addActionListener(this);
		boutonSortir.addActionListener(this);
	}
	
	private void setupFrame() {
		Container pane = getContentPane();
		pane.setLayout(new BorderLayout());
		
		/* -------------- entete et bas de page -------------- */
		/* entete */
		JPanel panelEntete = new JPanel();
		panelEntete.add(new JLabel("Ajouter une section"));
		pane.add(panelEntete, BorderLayout.NORTH);
		/* bas de page */
		JPanel panelBasDePage = new JPanel();
		panelBasDePage.add(boutonAjouter);
		panelBasDePage.add(boutonSortir);
		pane.add(panelBasDePage, BorderLayout.SOUTH);
		
		/* formulaire */
		GridLayout gl = new GridLayout(0, 2);
		JPanel pf = new JPanel(gl);
		pf.add(new JLabel("President: "));
		pf.add(inputPresident);
		pane.add(pf);
		
		/* marge de gauche et droite */
		pf.setBorder(new EmptyBorder(10, 10, 10, 10));
		
		setModal(true);
	}

	

	

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == boutonSortir) {
			this.setVisible(false);
		}
		if (e.getSource() == boutonAjouter) {
			DaoSection ds = daoFactory.getDaoSection();
			Section section = ds.requestByPresident(inputPresident.getText());
			if (section != null) {
				JOptionPane.showMessageDialog(this, "Erreur de saisie ! Ce président s'occupe deja d'une autre section !");
			}
			else {
				section = new Section(inputPresident.getText());
				ds.createSection(section);
				inputPresident.setText("");
				isw.repaint();
				setVisible(false);
			}
		}
	}

	

}
